package com.bms.cfcmms.controller.system;

import com.bms.cfcmms.controller.BaseController;
import com.bms.cfcmms.model.share.ApplicationInfo;
import com.bms.cfcmms.util.SpringUtils;
import com.bms.cfcmms.util.annotation.TableDefine;
import com.bms.cfcmms.model.share.SystemInfo;
import com.bms.cfcmms.service.ModuleService;
import com.bms.cfcmms.service.SystemService;
import com.bms.cfcmms.util.ActionResult;
import com.bms.cfcmms.util.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by carl on 2016/4/11.
 */
@RestController
@RequestMapping("/systemframe")
public class SystemFrameController extends BaseController {

    final Logger log = LoggerFactory.getLogger(SystemFrameController.class);
    public static final String SYSTEM_UPDATAE_PACK_PATH = "updatePack" + File.separator;

    @Resource
    private SystemService systemFrameService;



    /**
     * 添加模块的入口函数.
     * <p>
     * <p>函数里面处理每个模块的相关信息，比如：
     * 权限信息，相关表的信息等
     * </p>
     *
     * @param moduleName 模块的名称
     * @return 返回一个字符串信息，成功返回null
     */
    @RequestMapping(value = "/addmodule",
            produces = "application/json;text/plain;charset=UTF-8")
    public String addModuleWithName(String moduleName) {

        Class<?> moduleClass = ModuleService.getModuleBeanClass(moduleName);
        if (moduleClass == null) {
            log.error("未在指定的包中找到类:" + moduleName + "!");
            return "未在指定的包中找到类:" + moduleName + "!";
        }

        TableDefine tableDefine = (TableDefine) moduleClass.getAnnotation(TableDefine.class);
        if (tableDefine == null) {
            log.error("未在指定的类中找到tableDefine的标注定义");
            return "未在指定的类中找到tableDefine的标注定义";
        }

        systemFrameService.addModuleWithName(moduleName, moduleClass, tableDefine);

        return null;
    }

    /**
     * 文件上传.
     * <p/>
     * 系统升级时用到的文件上传
     *
     * @param req  HttpServletRequest
     * @param resp HttpServletResponse
     * @param file 上传的文件
     * @return 是否成功
     */
    @RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
    public ActionResult systemUpdate(HttpServletRequest req, HttpServletResponse resp,
                                     MultipartFile file){

        ActionResult result = new ActionResult();
        String fineName = file.getOriginalFilename();
        String filePath = SysUtils.getInstallPath() + SYSTEM_UPDATAE_PACK_PATH + fineName;
        File tmpFile = new File(filePath);
        if (fineName != null && !file.isEmpty()) {
            try {
                FileCopyUtils.copy(file.getBytes(), tmpFile);
                systemFrameService.handleUpdatePack(tmpFile);
                result.setSuccess(true);
            } catch (IOException e) {
                Object[] params = {fineName, e.toString()};
                log.error(SpringUtils.getMessage("file.upload.fail", params), e);
                result.setSuccess(false);
            }
        }
        return result;
    }

    /**
     * 获取系统信息.
     *
     * @param request 请求
     * @return ApplicationInfo对象
     */
    @RequestMapping("/applicationinfo.do")
    public ActionResult getApplicationInfo(HttpServletRequest request) {

        ApplicationInfo applicationInfo = systemFrameService.getApplicationInfo(request);
        ActionResult actionResult = new ActionResult();
        actionResult.setSuccess(true);
        actionResult.setData(applicationInfo);
        return actionResult;
    }

    /**
     * 获取系统配置.
     *
     * @return 系统配置对象
     */
    @RequestMapping("/querySystemInfo.do")
    public SystemInfo querySystemInfo() {

        return new SystemInfo();
    }
}
